package com.guyao.gynews;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.guyao.gynews.activity.StudentInfoActivity;
import com.guyao.gynews.adapter.ClassMateAdapter;
import com.guyao.gynews.bean.ClassMate;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ListView classMateListView;
    private ArrayList<ClassMate.ListBean> listBeans;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
    }
    private void initView(){
        classMateListView = findViewById(R.id.clasamate_listview);
        classMateListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(parent.getContext(), StudentInfoActivity.class);
                intent.putExtra("id",listBeans.get(position).getId()+"");
                startActivity(intent);


            }
        });

    }
    private void initData(){
        String url = "http://10.1.53.1:8080/freshmenapp/classmate/listbyclassname";

        //Gson用来数据解析Volley网络请求
        final Gson gson = new Gson();
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        JSONObject jsonObject = new JSONObject();
        try{
            jsonObject.put("className","18移动1班");
        }catch (JSONException e){
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {



            @Override
            public void onResponse(JSONObject jsonObject) {

                ClassMate classMate = gson.fromJson(jsonObject.toString(),ClassMate.class);
                listBeans = (ArrayList<ClassMate.ListBean>) classMate.getList();
                classMateListView.setAdapter(new ClassMateAdapter(getApplicationContext(),listBeans));
                Toast.makeText(MainActivity.this,"获取成功",Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MainActivity.this,"网络失败",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);


    }
}
